package com.lbt.vod.controller;

import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import com.lbt.commonuntils.R;
import com.lbt.servicebase.exceptionhandler.GuliException;
import com.lbt.vod.service.VodService;
import com.lbt.vod.untils.ConstantVodUntils;
import com.lbt.vod.untils.InitVodCilent;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(description = "上传视频接口")
@RestController
//@CrossOrigin
@RequestMapping("/eduvod/video")
public class VodController {
    @Autowired
    VodService vodService;

    @ApiOperation(value = "本地视频上传")
    @PostMapping("/uploadAlyiVideo")
    public R uploadAlyVideo(MultipartFile file){
        //返回视频上传生成的ID
        System.out.println("视频上传启动/////////////////");
       String videoId=vodService.uploadVideoAly(file);
        return R.ok().data("videoId",videoId);
    }
    @ApiOperation(value = "根据视频id删除视频")
    @DeleteMapping("removeAlyVideo/{id}")
    public R removeVideoByID(@PathVariable String id){
       try{
           DefaultAcsClient client = InitVodCilent.initVodClient("LTAI4GHNASCskmXV3TtdmTem", "Iejm8ZYaJD3ThCl6sePJDgI6Ks4zqm");
           DeleteVideoRequest request=new DeleteVideoRequest();
           request.setVideoIds(id);
           client.getAcsResponse(request);
       }catch (Exception e){
           e.printStackTrace();
           throw new GuliException(20001,"删除视频失败");
       }
       return R.ok();
    }
    //删除多个视频
    @DeleteMapping("/delete-batch")
    public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList){
        vodService.removeMoreAlyVideo(videoIdList);
        return R.ok();
    }
    @ApiOperation(value = "获取视频播放凭证")
    @GetMapping("getPlayAuth/{id}")
    public R getPalyAuth(@PathVariable String id){
        try{ //创建初始化对象
            DefaultAcsClient client = InitVodCilent.initVodClient("LTAI4GHNASCskmXV3TtdmTem","Iejm8ZYaJD3ThCl6sePJDgI6Ks4zqm");
            //创建获取凭证的resques response
            GetVideoPlayAuthRequest request=new GetVideoPlayAuthRequest();
            //设置id
            request.setVideoId(id);
            GetVideoPlayAuthResponse acsResponse = client.getAcsResponse(request);
            String playAuth = acsResponse.getPlayAuth();
            return R.ok().data("playAuth",playAuth);
        }catch (Exception e){
            throw new GuliException(20001,"获取视频播放凭证失败");
        }


    }
}
